using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.IsolatedStorage;

namespace Meteorite
{
    public class UserData
    {
        static UserData myself = null;
        const string UserDataFilename = "UserData.txt";

        Dictionary<string, int> KeyList = new Dictionary<string, int>();

        UserData()
        {
            myself = this;
        }
        public static UserData Instance()
        {
            if (null == myself)
            {
                myself = new UserData();
            }
            return myself;
        }

        public void SetValue(string Key, int Value)
        {
            if (!KeyList.ContainsKey(Key))
            {
                KeyList.Add(Key, Value); 
            }
            else
            {
                KeyList[Key] = Value;
            }
        }
        
        public void AddValue(string Key, int Increament)
        {
            if (!KeyList.ContainsKey(Key))
            {
                KeyList.Add(Key, Increament);
            }
            else
            {
                KeyList[Key] = KeyList[Key]+Increament;
            }
        }
        
        public int GetValue(string Key)
        {
            if (!KeyList.ContainsKey(Key))
            {
                KeyList.Add(Key, 0);
            }
            return KeyList[Key];
        }
        public void Load()
        {
            using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isolatedStorageFile.FileExists(UserDataFilename))
                {
                    using (IsolatedStorageFileStream isfs =
                        isolatedStorageFile.OpenFile(UserDataFilename, FileMode.Open))
                    {
                        using (StreamReader reader = new StreamReader(isfs))
                        {
                            KeyList.Clear();
                            while (!reader.EndOfStream)
                            {
                                string name = reader.ReadLine();
                                string score = reader.ReadLine();
                                SetValue(name, int.Parse(score));
                            }
                        }
                    }
                }
            }
        }
        public void Save()
        {
            using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isfs =
                    isolatedStorageFile.OpenFile(UserDataFilename, FileMode.Create))
                {
                    using (StreamWriter writer = new StreamWriter(isfs))
                    {
                        foreach (KeyValuePair<string, int> a in KeyList)
                        {       
                            writer.WriteLine(a.Key);
                            writer.WriteLine(a.Value);
                        }
                    }
                }
            }
        }
    }
}
